<?php
    /**
    * List all real esate project in this module
    * this action work with all user, include guest
    *
    * SEO support
    * 
    * Support : list all project in all collection
    *           List all project in on collection
    * 
    * @author trungdt
    * @since June 25, 2011
    * 
    * Change logs
    * 
    */
    
    if (!defined('EXPONENT')) exit('');
    // permission check: for admin only
    global $db;

    // 8/24/2009: Immanuel192; get module config
    $config=$db->selectObject("duanmodule_config","location_data = '".serialize($loc)."'");

    $max_item=25; // default 25 project. dont need config hehe
    
    $sql_where="pstate = 1";
    
    // in case that we have permission, must should show all project item which not appeared
    if (exponent_permissions_check('administrate',$loc) || exponent_permissions_check('edit',$loc) || exponent_users_isAdmin())
    {
        $sql_where="1"; // remove filter pstate = 1
    }
    
    // get all projects here
    $count_post = $db->countObjects('duan',"location_data='".serialize($loc)."'  AND {$sql_where}");

    // page split
    // cal how many posts
    $page_count=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $page_count++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$page_count)) $page=1;
    $start_pos=($page-1)*$max_item;

    
    $sql="SELECT {$db->prefix}duan.*, {$db->prefix}provinces.short_name pshortname, {$db->prefix}provinces.name pname, {$db->prefix}provinces_wards.shortname wshortname, {$db->prefix}provinces_wards.name wname
        FROM {$db->prefix}duan 
        LEFT JOIN {$db->prefix}provinces ON {$db->prefix}provinces.id = {$db->prefix}duan.province_id
        LEFT JOIN {$db->prefix}provinces_wards ON {$db->prefix}provinces_wards.id = {$db->prefix}duan.province_ward_id
    
    WHERE location_data='".serialize($loc)."' AND {$sql_where} ORDER BY pstate ASC, created_on DESC LIMIT {$start_pos},{$max_item}
    ";
    
    $projects= $db->selectObjectsBySql($sql);
    
    $projects=duanmodule::_ProjectProcess($projects);
    
    // page title
    $title="Danh sách các dự án";
    // set og, page title
    $head_title="Các dự án";
    _ab_theme_action_register_title($head_title,$page,1);
    
    //
    $template = new template('duanmodule',"_duan_list",$loc);
    $template->register_permissions(array('administrate','configure','edit','create','delete'),$loc);
    $template->assign('projects', $projects);
    $template->assign('page_title', $title);
    
    $template->assign('pages',$page_count);
    $template->assign('page',$page);
    if (!(isset($_REQUEST['flow_has_set']) && ($_REQUEST['flow_has_set']==1)))
        exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
    $template->output();
?>
